package com.simtop.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.User;
import com.simtop.service.SwrelationService;
import com.simtop.util.JwtUtil;
import com.simtop.vo.Devicec;
import com.simtop.vo.Swrelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("swrelation")
@CrossOrigin
public class SwrelationController {
    @Autowired
    private SwrelationService service;
    @PostMapping("findlaneid")
    @ResponseBody
    public ServerResponse findlaneid(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer laneid=jsonObject.getInteger("laneid");
            List<Swrelation> swreadList = service.findlaneid(laneid);

            return ServerResponse.createBySuccess(swreadList);
        }
        catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }
    @PostMapping("findall")
    @ResponseBody
    public ServerResponse findall(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer laneid=jsonObject.getInteger("laneid");
            Integer deviceid=jsonObject.getInteger("deviceid");
            List<Devicec> swreadList = service.findall(laneid,deviceid);
            return ServerResponse.createBySuccess(swreadList);
        }
        catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }
    @PostMapping("saveupdate")
    @ResponseBody
    public ServerResponse saveupdate(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Float  tabledata=null;
            Float  rangedown=null;
            Float  rangeup=null;
            Float  x= null;
            Float  y=null;
            Float  k=null;
            Float  g=null;
            Float  level=null;
            Float  vertical=null;
            Float  core=null;
            Integer laneid=jsonObject.getInteger("laneid");
            Integer deviceid=jsonObject.getInteger("deviceid");
            tabledata=jsonObject.getFloat("tabledata");
            rangedown=jsonObject.getFloat("rangedown");
            rangeup=jsonObject.getFloat("rangeup");
            x=jsonObject.getFloat("x");
            y=jsonObject.getFloat("y");
            k=jsonObject.getFloat("k");
            g=jsonObject.getFloat("g");
            level=jsonObject.getFloat("level");
            vertical=jsonObject.getFloat("vertical");
            core=jsonObject.getFloat("core");
            Integer start=jsonObject.getInteger("start");
            Integer end=jsonObject.getInteger("end");
            Integer startcontroler=jsonObject.getInteger("startcontroler");
            Integer endcontroler=jsonObject.getInteger("endcontroler");
            List<Devicec> swreadList = service.findall(laneid,deviceid);
            if (swreadList.size()==0){
                service.addevicec(laneid,deviceid,tabledata,rangedown,rangeup,x,y,k,g,level,vertical,core,start,end,startcontroler,endcontroler);
                return ServerResponse.createBySuccess("添加成功");
            }else {
                service.updatedevicec(laneid,deviceid,tabledata,rangedown,rangeup,x,y,k,g,level,vertical,core,start,end,startcontroler,endcontroler);
                return ServerResponse.createBySuccess("修改内容成功");
            }
        }
        catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }
}
